import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:flutter/material.dart";

enum ButtonType {
  danger,
  dangerOutline,
  normal,
  primary,
  primaryLight,
  primaryOutline,
  success,
  successOutline,
}

enum ButtonSize {
  small,
  medium,
  large,
}

class Button extends StatelessWidget {
  final double radius;
  final ButtonSize size;
  final ButtonType type;
  final void Function()? onPressed;
  final Widget child;

  const Button({
    super.key,
    this.radius = 6,
    this.size = ButtonSize.medium,
    this.type = ButtonType.primary,
    this.onPressed,
    required this.child,
  });

  Size _getSize() {
    switch (size) {
      case ButtonSize.small:
        return const Size.fromHeight(28);
      case ButtonSize.large:
        return const Size.fromHeight(44);
      default:
        return const Size.fromHeight(36);
    }
  }

  @override
  Widget build(BuildContext context) {
    bool disabled = onPressed == null;
    return ElevatedButton(
      style: ButtonStyle(
        textStyle: WidgetStateProperty.resolveWith<TextStyle>((states) {
          double fontSize = size == ButtonSize.small ? 14 : 16;
          return TextStyle(fontSize: fontSize);
        }),
        backgroundColor: WidgetStateProperty.resolveWith<Color>((states) {
          if (disabled) return ThemeVars.colorDisabled;
          if (type == ButtonType.danger) return ThemeVars.colorError;
          if (type == ButtonType.primary) return ThemeVars.colorPrimary;
          if (type == ButtonType.primaryLight) {
            return ThemeVars.colorPrimaryLight;
          }
          if (type == ButtonType.success) return ThemeVars.colorSuccess;
          return Colors.white;
        }),
        foregroundColor: WidgetStateProperty.resolveWith<Color>((states) {
          if (disabled) return Colors.white;
          if (type == ButtonType.dangerOutline) return ThemeVars.colorError;
          if (type == ButtonType.normal) return ThemeVars.colorPrimary;
          if (type == ButtonType.primaryLight ||
              type == ButtonType.primaryOutline) return ThemeVars.colorPrimary;
          if (type == ButtonType.successOutline) return ThemeVars.colorSuccess;
          return Colors.white;
        }),
        fixedSize: WidgetStateProperty.all(_getSize()),
        overlayColor: WidgetStateProperty.resolveWith<Color?>((states) {
          Color color = Colors.white;
          if (type == ButtonType.dangerOutline) color = ThemeVars.colorError;
          if (type == ButtonType.normal) color = ThemeVars.colorText;
          if (type == ButtonType.primaryLight ||
              type == ButtonType.primaryOutline) color = ThemeVars.colorPrimary;
          if (type == ButtonType.successOutline) color = ThemeVars.colorSuccess;
          return color.withOpacity(0.15);
        }),
        shadowColor: WidgetStateProperty.all(Colors.transparent),
        shape:
            WidgetStateProperty.resolveWith<RoundedRectangleBorder>((states) {
          BorderRadiusGeometry borderRadius = BorderRadius.circular(radius);
          BorderSide side = BorderSide.none;
          if (!disabled) {
            Color? color;
            if (type == ButtonType.dangerOutline) color = ThemeVars.colorError;
            if (type == ButtonType.primaryOutline) {
              color = ThemeVars.colorPrimary;
            }
            if (type == ButtonType.successOutline) {
              color = ThemeVars.colorSuccess;
            }
            if (color != null) side = BorderSide(color: color);
          }
          return RoundedRectangleBorder(borderRadius: borderRadius, side: side);
        }),
      ),
      onPressed: onPressed,
      child: child,
    );
  }
}
